package com.huanxi.joytravel.bases;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.huanxi.joytravel.R;
import com.huanxi.joytravel.utils.NetWatchdogUtils;
import com.huanxi.joytravel.widgets.ProgressLinearLayout;
import com.youth.banner.util.LogUtils;


/**
 * Fragment预加载问题的解决方案：
 * 1.可以懒加载的Fragment
 * 2.切换到其他页面时停止加载数据（可选）
 * Created by yuandl on 2016-11-17.
 * blog ：http://blog.csdn.net/linglongxin24/article/details/53205878
 */

public abstract class BaseLazyLoadFragment extends BaseFragment {
    /**
     * 视图是否已经初初始化
     */

    private boolean isVisible;                  //是否可见状态
    private boolean isPrepared;                 //标志位，View已经初始化完成。
    private boolean isFirstLoad = true;         //是否第一次加载

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    protected void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirstLoad) {
            return;
        }
        isFirstLoad = false;
        if (!NetWatchdogUtils.hasNet(getContext())) {
            showShortToast("网络未连接");
        }
        initLazyLoadData();
    }

    public void showEmpty(ProgressLinearLayout progress, String errorTitle) {
        if (NetWatchdogUtils.hasNet(mContext)) {
            progress.showEmpty(R.mipmap.null_search, errorTitle, "");
        } else {
            progress.showEmpty(R.mipmap.null_net, getNetErrStrirng(), "");
        }
    }

    public void showError(ProgressLinearLayout progress, String errorTitle) {
        if (NetWatchdogUtils.hasNet(getActivity())) {
            progress.showError(R.mipmap.ic_empty_defalt, errorTitle, "", "点击重试", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initLazyLoadData();
                }
            });
        } else {
            progress.showError(R.mipmap.ic_zw_no_net, getNetErrStrirng(), "", "点击重试", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    initLazyLoadData();
                }
            });
        }

    }

    protected abstract void initLazyLoadData();

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInVisible();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInVisible();
        }
    }

    protected void onInVisible() {
    }


    private void onVisible() {
        lazyLoad();
        LogUtils.i("base-onVisible-调用lazyLoad");
    }

    protected void showToast(String message) {
        if (!TextUtils.isEmpty(message)) {
            Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 获取设置的布局
     *
     * @return
     */
    protected View getContentView() {
        return rootView;
    }

    /**
     * 找出对应的控件
     *
     * @param id
     * @param <T>
     * @return
     */
    protected <T extends View> T findViewById(int id) {
        return (T) getContentView().findViewById(id);
    }


    /**
     * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以调用此方法
     */
    protected void stopLoad() {
    }
}
